package com.gking.resourcePool.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gking.resourcePool.common.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Map;


/**
 * 发送http请求的工具类
 */

@Slf4j
@Component
public class RestTemplateUtil {

    // 定义要请求的 URL
    private static String baseUrl;

    @Value("${remoteUrl}")
    private String baseUrlTemp;

    @Bean
    public void initBaseUrl() {
        RestTemplateUtil.baseUrl = this.baseUrlTemp;
    }


    // 创建 RestTemplate 对象
    private static final RestTemplate restTemplate = new RestTemplate();

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 格式化获取到的JSON数据
     *
     * @param jsonString json字符串
     * @return s
     */
    public static ResultResponse parseJSON(String jsonString) {
        try {
            return objectMapper.readValue(jsonString, ResultResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 发送 GET 请求，并获取响应
     *
     * @param path            请求路径
     * @param requestParamStr param 参数字符 "?param1=value1..."
     * @param header          请求头
     */
    public static HttpEntity<String> httpGET(String path, String requestParamStr,
                                             Map<String, String> header) {
        HttpHeaders headers = new HttpHeaders();
        header.forEach(headers::set);
        headers.setContentType(MediaType.APPLICATION_JSON);

        String requestUrl = baseUrl + path + requestParamStr;
        HttpEntity<Map<String, String>> getEntity = new HttpEntity<>(null, headers);
        return restTemplate.exchange(requestUrl, HttpMethod.GET, getEntity, String.class);
    }

    /**
     * 发送 POST 请求，并获取响应
     *
     * @param path            请求路径
     * @param requestParamStr param 参数字符 "?param1=value1..."
     * @param requestBody     requestBody 参数
     * @param header          请求头
     * @return s
     */
    public static ResponseEntity<String> httpPOST(String path, String requestParamStr,
                                                  Map<String, String> requestBody,
                                                  Map<String, String> header) {
        HttpHeaders headers = new HttpHeaders();
        header.forEach(headers::set);
        headers.setContentType(MediaType.APPLICATION_JSON);

        String requestUrl = baseUrl + path + requestParamStr;
        HttpEntity<Map<String, String>> postEntity = new HttpEntity<>(requestBody, headers);
        return restTemplate.exchange(requestUrl, HttpMethod.POST, postEntity, String.class);
    }

    /**
     * 发送 PUT 请求，并获取响应
     *
     * @param path            请求路径
     * @param requestParamStr param 参数字符 "?param1=value1..."
     * @param requestBody     requestBody 参数
     * @param header          请求头
     * @return s
     */
    public static HttpEntity<String> httpPUT(String path, String requestParamStr,
                                             Map<String, String> requestBody,
                                             Map<String, String> header) {
        HttpHeaders headers = new HttpHeaders();
        header.forEach(headers::set);
        headers.setContentType(MediaType.APPLICATION_JSON);

        String requestUrl = baseUrl + path + requestParamStr;
        HttpEntity<Map<String, String>> putEntity = new HttpEntity<>(requestBody, headers);
        return restTemplate.exchange(requestUrl, HttpMethod.PUT, putEntity, String.class);
    }


}
